import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class AlertDialogPage extends StatefulWidget {
  @override
  _AlertDialogPageState createState() => _AlertDialogPageState();
}

class _AlertDialogPageState extends State<AlertDialogPage> {
  void _alertDialog() async {
    var result = await showDialog(
        context: context,
      builder: (context) {
          return AlertDialog(
            title: Text('提示'),
            content: Text('确定要删除吗？'),
            actions: <Widget>[
              RaisedButton(
                child: Text('取消'),
                onPressed: (){},
              ),
              RaisedButton(
                child: Text('确定'),
                color: Colors.red,
                textColor: Colors.white,
                onPressed: (){
                  Navigator.of(context).pop('ok');//会把值传给接受的参数
                },
              )
            ],
          );
      }
    );
    print(result); // 打印接受到的值
  }

  void _selectDialog() async {
    var result = await showDialog(
        context: context,
      builder: (context){
          return SimpleDialog(
            title: Text('选择你的性别'),
            children: <Widget>[
              SimpleDialogOption(
                child: Text('男'),
                onPressed: () {
                  Navigator.of(context).pop('男');
                },
              ),
              Divider(),
              SimpleDialogOption(
                child: Text('女'),
                onPressed: () {
                  Navigator.of(context).pop('女');
                },
              )
            ],
          );
      }
    );
    print(result);
  }
  void _actionSheetDialog() {
    showModalBottomSheet(context: context, builder: (context) {
      return Container(
        height: 250,
        child: Column(
          children: <Widget>[
            ListTile(
              title: Text('分享到微信'),
              onTap: () {
                Navigator.of(context).pop();
              },
            ),
            Divider(),
            ListTile(
              title: Text('分享到QQ'),
              onTap: () {
                Navigator.of(context).pop();
              },
            ),
            Divider(),
            ListTile(
              title: Text('分享到微博'),
              onTap: () {
                Navigator.of(context).pop();
              },
            )
          ],
        ),
      );
    });
  }
  void _toast() {
    Fluttertoast.showToast(
        msg: "你好Flutter",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIos: 1,
        backgroundColor:  Color.fromRGBO(0, 0, 0, .8),
        textColor: Colors.white,
        fontSize: 16.0
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Alert & Dialog'),
      ),
      body: Column(
        children: <Widget>[
          RaisedButton(
            child: Text('alert弹出框-AlertDialog'),
            onPressed: _alertDialog,
          ),
          RaisedButton(
            child: Text('select弹出框-SelectDialog'),
            onPressed: _selectDialog,
          ),
          RaisedButton(
            child: Text('ActionSheet弹出框-ActionSheetDialog'),
            onPressed: _actionSheetDialog,
          ),
          RaisedButton(
            child: Text('toast-fluttertoast第三方库'),
            onPressed: _toast,
          )
        ],
      ),
    );
  }
}
